package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    //查询所有部门信息
    @GetMapping
    public Result list(){
        List<Dept>depts=deptService.list();
        return Result.success(depts);
    }

    //新增部门
    @PostMapping
    public Result save(@RequestBody Dept dept) {
        //调用业务层方法进行保存部门信息
        deptService.save(dept);
        return Result.success();
    }

    //删除部门
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        //调用service删除部门
        deptService.deleteById(id);
        //响应成功
        return Result.success();
    }

    //根据id查询部门
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        //调用service查询部门
        Dept dept = deptService.getById(id);
        //响应部门数据
        return Result.success(dept);
    }

    //修改部门
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        //调用service修改部门
        deptService.update(dept);
        //响应成功
        return Result.success();
    }





}
